<template>
  <div>
    <sticky :class-name="'sub-navbar'" :z-index="10">
      <el-button @click="$router.back()" type="info">
        返回全部产品列表
      </el-button>
    </sticky>

    <el-card>
      <el-alert
        :closable="false"
        :title="`${linkFrom}基本信息新增`"
        effect="dark"
        show-icon
        style="margin-bottom: 15px;"
        type="success"
      />
      <!--  产品基本信息,这个两边区别不是很大  -->
      <ProductBaseInfoForm
        :modifyData="modifyData"/>
    </el-card>

    <!--      // linkFrom linkTo-->
    <!--  产品列表,已有的,不需要分页 -->
    <!--  产品配置  -->

    <el-card
      v-if="modifyData.ProductType!==productTypesEnum['组件']"
        style="margin-top: 15px;height: 55vh">
      <!--      <ProductRelateConfig-->
      <!--        :modifyData="modifyData"-->
      <!--        :tableData="hasRelateList"-->
      <!--        @tableDataUpdate="tableDataUpdate"-->
      <!--       />-->

      <el-alert
        :closable="false"
        :description="`请先添加完成${linkFrom}基本信息后添加${linkTo}`"
        :title="`配置${linkTo}`"
        effect="dark"
        show-icon
        type="info"
      />

    </el-card>
  </div>

</template>

<script>

  //  产品列表
  import AzureDynamicTable from '@/components/ATheFridayClub/mDynamicTable/AzureDynamicTable'
  import { ERR_OK } from '@/utils/commons'
  import layoutMixin from '@/mixins/layoutMixin'
  import Sticky from '@/components/Sticky/index'
  import AButton from '@/components/ATheFridayClub/AButton'
  import { createProductFactory, productBaseCols } from '@/views/baseproduct/mixins/productUtil'
  import ShowAllProductList from '@/views/baseproduct/components/product/ShowAllProductListDrawer'
  import ProductBaseInfoForm from '@/views/baseproduct/components/product/ProductBaseInfoForm'
  import { productTypesEnum } from '@/enums/product'
  import productMixin from '@/views/baseproduct/components/product/productMixin'
  import ProductRelateConfig from '@/views/baseproduct/components/product/ProductRelateConfig'
  import { GenerateProductCode, ModifyProduct, ModifyProductMaterial } from '@/api/productInformation'
  import { objFirstKeyLowerCase } from '@/utils/fridayClub/objectUtil'

  const baseInfoFactory = ({ code, productType } = {}) => ({
    // productId: 0,
    // categoricalId: 0,
    // code,
    // cutFilePath: '',
    // describe: '',
    // name: '',
    // productPrintTypeId: 0,
    // productShopType: 1,
    // productSize: '',
    // productType,  //  组合产品 产品 组件
    // singleWeight: 0,
    // sizeDetailId: 0,
    // weight: 0,

    //  与返回的一致  ?????
    Code: code,
    ProductType: productType,
    CutFilePath: null,
    Describe: '',
    ProductId: 0,
    ProductName: '',
    ProductPrintType: '',
    ProductPrintTypeId: '',
    ProductShopType: '',
    ProductSize: '',
    Status: 1,
    SingleWeight: '',
    Weight: 10
  })

  // { value: 2, label: '组合产品' }
  // { value: 1, label: '产品' },
  // { value: 0, label: '组件' },
  // const createMapper = new Map([
  //   [2, 1],
  //   [1, 0],
  //   [0, 0]
  // ])

  export default {
    name: 'createProductInfo',
    components: {
      ProductRelateConfig,
      ProductBaseInfoForm,
      ShowAllProductList, AButton, Sticky
    },
    mixins: [layoutMixin, productMixin],
    data() {
      return {
        productTypesEnum,
        modifyData: {},
        hasRelateList: Array.of() //  产品目前的组件

      }
    },

    computed: {},
    async created() {
      //  修改 route :: title 为 组件,产品,套系

      // const productType = productTypesEnum['产品']
      const { ProductType: productType } = this.$route.query

      console.log(productType);
      console.log(this.modifyData);

      // *  @config2MappingValue 配置关联关系值
      // *  @config2MappingName  配置关联关系翻译
      //  创建基本信息工厂
      const code = await this.GenerateProductCode({ productType })
      this.modifyData = baseInfoFactory({
        code, //  帮助用户生成产品编码,暂时不考虑失败
        productType: +productType  //  默认创建有一个类型,组件,套系,产品
      })

    },
    methods: {

      //  接受派发的事件,获取到组件的ids
      //  1 保存关联信息  产品间关系编辑
      // POST /api/Product/ModifyProductComponent
      saveLinkRelate() {

      },
      async GenerateProductCode({ productType } = {}) {
        try {
          const { tableData } = this
          const { Code, Result } = await GenerateProductCode({ productType })
          if (Code !== ERR_OK) {
            this.$message.warning('生成编码失败')
          }
          return Result
        } catch (e) {
          console.log(e)
        }
      },
      tableDataUpdate(newTableData) {
        this.hasRelateList = newTableData
      }

    }
  }
</script>

<style lang="scss" scoped>

</style>
